﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using AuctionHelper.Entity;
using System.IO;

namespace AuctionHelper.Core
{
    /// <summary>
    /// 捕获辅助类
    /// </summary>
    public class CaptureHelper
    {
        [DllImport("gdi32.dll")]
        protected static extern IntPtr CreateDC(

         string lpszDriver,        // driver name驱动名

         string lpszDevice,        // device name设备名

         string lpszOutput,        // not used; should be NULL

         IntPtr lpInitData  // optional printer data

         );

        [DllImport("gdi32.dll")]
        protected static extern int BitBlt(

         IntPtr hdcDest, // handle to destination DC目标设备的句柄

         int nXDest,  // x-coord of destination upper-left corner目标对象的左上角的X坐标

         int nYDest,  // y-coord of destination upper-left corner目标对象的左上角的Y坐标

         int nWidth,  // width of destination rectangle目标对象的矩形宽度

         int nHeight, // height of destination rectangle目标对象的矩形长度

         IntPtr hdcSrc,  // handle to source DC源设备的句柄

         int nXSrc,   // x-coordinate of source upper-left corner源对象的左上角的X坐标

         int nYSrc,   // y-coordinate of source upper-left corner源对象的左上角的Y坐标

         UInt32 dwRop  // raster operation code光栅的操作值

         );



        [DllImport("gdi32.dll")]
        protected static extern IntPtr CreateCompatibleDC(

         IntPtr hdc // handle to DC

         );



        [DllImport("gdi32.dll")]
        protected static extern IntPtr CreateCompatibleBitmap(

         IntPtr hdc,        // handle to DC

         int nWidth,     // width of bitmap, in pixels

         int nHeight     // height of bitmap, in pixels

         );



        [DllImport("gdi32.dll")]
        protected static extern IntPtr SelectObject(

         IntPtr hdc,          // handle to DC

         IntPtr hgdiobj   // handle to object

         );



        [DllImport("gdi32.dll")]
        protected static extern int DeleteDC(

         IntPtr hdc          // handle to DC

         );



        [DllImport("user32.dll")]
        protected static extern bool PrintWindow(

         IntPtr hwnd,               // Window to copy,Handle to the window that will be copied. 

         IntPtr hdcBlt,             // HDC to print into,Handle to the device context. 

         UInt32 nFlags              // Optional flags,Specifies the drawing options. It can be one of the following values. 

         );



        [DllImport("user32.dll")]
        protected static extern IntPtr GetWindowDC(

         IntPtr hwnd

         );

        [DllImport("user32.dll")]
        protected static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);


        [DllImport("gdi32.dll")]
        protected static extern int DeleteObject(
        IntPtr hdc
       );

        [StructLayout(LayoutKind.Sequential)]
        protected struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        /// <summary>
        /// 截屏API
        /// </summary>
        /// <param name="window"></param>
        /// <returns></returns>
        public static Bitmap GetScreenAPI(IntPtr window)
        {
            var rect = new RECT();
            GetWindowRect(window, out rect);
            var windowDC = GetWindowDC(window);
            var hBitmap = CreateCompatibleBitmap(windowDC, rect.right - rect.left, rect.bottom - rect.top);
            var hMemDC = CreateCompatibleDC(windowDC);
            SelectObject(hMemDC, hBitmap);
            PrintWindow(window, hMemDC, 0);
            var bitmap = Bitmap.FromHbitmap(hBitmap);
            DeleteDC(windowDC);
            DeleteObject(hBitmap);
            DeleteDC(hMemDC);
            return bitmap;
        }

        /// <summary>
        /// 截屏
        /// </summary>
        /// <returns></returns>
        public static void GetScreen(string imageFilePath)
        {
            var rect = Screen.PrimaryScreen.Bounds;
            using (var bitmap = new Bitmap(rect.Width, rect.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(rect.Width, rect.Height));
                    IntPtr dc2 = g.GetHdc();
                    g.ReleaseHdc(dc2);
                    g.Dispose();
                }
                bitmap.Save(imageFilePath, ImageFormat.Tiff);
                bitmap.Dispose();
            }
        }

        /// <summary>
        /// 截屏并裁切
        /// </summary>
        /// <returns></returns>
        public static void GetScreenAndCut(string imageFilePath, Coordinate position)
        {
            var rect = Screen.PrimaryScreen.Bounds;
            using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(rect.Width, rect.Height));
                    IntPtr dc2 = g.GetHdc();
                    g.ReleaseHdc(dc2);
                    g.Dispose();
                }

                bitmap.Save(imageFilePath, ImageFormat.Tiff);
                bitmap.Dispose();
            }
        }

        /// <summary>
        /// 裁切图片
        /// </summary>
        /// <param name="memoryStream"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static MemoryStream CutBitmap(MemoryStream memoryStream, Coordinate position)
        {
            var memoryStreamPart = new MemoryStream();
            var rect = new RectangleF(position.Left, position.Top, position.Right - position.Left, position.Bottom - position.Top);
            using (var bitmap = new Bitmap(memoryStream))
            {
                bitmap.Clone(rect, bitmap.PixelFormat).Save(memoryStreamPart, ImageFormat.Tiff);
                bitmap.Dispose();
            }
            return memoryStreamPart;
        }
    }
}
